using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

/* =====================================================================

  File:      ReviewAdd.aspx.cs for Adventure Works Cycles Storefront Sample
  Summary:   Collects user comments about a product and saves them
  Date:	     June 16, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

namespace Microsoft.Samples.SqlServer {

    public partial class ReviewAdd : System.Web.UI.Page
    {

    
//         public ReviewAdd() {
//             Page.Init += new System.EventHandler(Page_Init);
//         }

        //*******************************************************
        //
        // The Page_Load event on this page is used to fetch details
        // about the product to review.  It then updates UI elements
        // with the results.
        //
        // Note that the product to review is specified using
        // a querystring argument to the page.
        //
        //*******************************************************
           
        private void Page_Load(object sender, System.EventArgs e) {

            if (Page.IsPostBack != true) {

                // Obtain ProductID of Product to Review
                int productID = Int32.Parse(Request["productID"], CultureInfo.InvariantCulture);
        
                // Populate Product Name on Page
                ProductsDB products = new ProductsDB();
                ModelName.Text = products.GetProductDetails(productID, WorldReady.CurrentLanguage()).ModelName;
            
                // Store ProductID in Page State to use on PostBack
                ViewState["productID"] = productID;
            }
        }

        //*******************************************************
        //
        // The ReviewAddButton_Click event is used to add a new
        // review into the AdventureWorks Reviews database.
        //
        // Note that we are deliberately HtmlEncoding all text
        // values *before* adding them to the database.  This allows
        // us to prevent hackers from adding images or hyperlinks
        // into the message content.
        //
        //*******************************************************

        protected void ReviewAddButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {

            // Only add the review if all fields on the page are valid
            if (Page.IsValid == true) {
              
                // Obtain ProductID from Page State
                int productID = (int) ViewState["productID"];

				// Obtain Rating number of RadioButtonList
				int rating = 5;

				if (Rating4.Checked)
					rating = 4;
				else if (Rating3.Checked)
					rating = 3;
				else if (Rating2.Checked)
					rating = 2;
				else if (Rating1.Checked)
					rating = 1;
             
                // Add Review to ReviewsDB.  HtmlEncode before entry
                ReviewsDB review = new ReviewsDB();
                review.AddReview(productID, Server.HtmlEncode(Name.Text), Server.HtmlEncode(Email.Text), rating, Server.HtmlEncode(Comment.Text));
              
                // Redirect client back to the originating product details page
                Response.Redirect("ProductDetails.aspx?ProductID=" + productID);
            }
        }

        private void Page_Init(object sender, EventArgs e) {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

		#region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {    
            this.ReviewAddButton.Click += new System.Web.UI.ImageClickEventHandler(this.ReviewAddButton_Click);

        }
		#endregion

    }
}
